<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Stat_model extends CI_Model  {

	function _construct()
	{
      parent::_construct();
	}
	
	function get_stat_by_area($data, $area, $select=''){
		$query = $this->db->query("select 
									o.osId,
								    p.pNum,
							        p.pId,
    								p.eoId,
								    e.eoName,
								    e1.eoName as ADM,
								    a.countUch,
								    a.countAud $select
								from
								    point p
								        JOIN
								    eduobjects e ON (p.eoId = e.eoId and left(e.terplace, 2) = $area)
								        JOIN
								    eduobjects e1 ON (e1.eoId = e.eoParent)
								        JOIN
								    rassadkapunkt rp ON (rp.pId = p.pId and rp.subjId = '".$data['subjId']."' and rp.sesNum = ".$data['sesNum'].")
								        JOIN
								    audanduch a ON (a.pId = rp.pId and a.subjId = rp.subjId and a.sesNum = rp.sesNum)
								        left JOIN
								    operstat o ON (o.pId = p.pId and o.saId = '".$data['saId']."' and o.subjId = '".$data['subjId']."' and o.sesNum = ".$data['sesNum'].")
								where
								    p.saId = '".$data['saId']."'
								order by p.pNum");
		if ($query->num_rows()>0){
			$row = $query->result_array();
			return $row;
		}
	}
	
	function get_personal_by_pt($data){
		$query = $this->db->query("select distinct count(r.perId) as count 
                                    from rassadka r, point p, rassadkapunkt rp 
                                    where p.saId='".$data['saId']."' and 
                                    p.pId=rp.pId and rp.subjId='".$data['subjId']."' and rp.sesNum=".$data['sesNum']." and 
                                    r.postId in (1,2,3,4,5) and r.rpId=rp.rpId and r.rezerv=0 and 
                                    rp.pId='".$data['pId']."'");
        if ($query->num_rows()>0){
        	$row = $query->result_array();
        	return $row[0];
        }
	}
	
	public function get_notCame_by_subj ($osId) {
		$query = $this->db->query("select 
									    c.subjId, c.countNotCame, s.subjName
									from
									    countnotcame c
									        join
									    subjects s ON (s.subjId = c.subjId)
									where
									    osId = $osId");
		if ($query->num_rows() > 0) {
			$row = $query->result_array();
			return $row;
		}
	}
	
	public function get_not_came_stat($pId, $osId, $subjId) {
		$resp = array();
		$query = $this->db->query("select count(sId) as studReg from rassadkadetej where subjId=$subjId and pId='$pId'");
		if ($query->num_rows() > 0) {
			$row = $query->row();
			$resp['studReg'] = $row->studReg;
		} else {
			$resp['studReg'] = 0;
		}
		if (isset($osId)){
			$query = $this->db->query("select sum(countNotCame) as studNotCame from countnotcame where subjId=$subjId and osId=$osId");
			if ($query->num_rows() > 0) {
				$row = $query->row();
				$resp['studNotCame'] = $row->studNotCame;
			} else {
				$resp['studNotCame'] = 0;
			}				
		} else {
			$resp['studNotCame'] = 0;
		}
		return $resp;
	}
}